package com.rotsel.mh3udb.app.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.rotsel.mh3udb.app.Adapters.WeaponUpgradeAdapter;
import com.rotsel.mh3udb.app.Extra.Formatter;
import com.rotsel.mh3udb.app.ListModels.WeaponUpgradeModel;
import com.rotsel.mh3udb.app.R;
import com.rotsel.mh3udb.app.lists.WeaponList;
import com.rotsel.mh3udb.app.lists.WeaponUpgradeList;
import com.rotsel.mh3udb.app.models.ElementModel;
import com.rotsel.mh3udb.app.models.WeaponModel;

import java.util.ArrayList;

/**
 * Created by rots on 14.02.14.
 */
public class WeaponDetailsActivity extends BaseActivity {

    private View weaponView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weapon_view);

        Bundle extras = getIntent().getExtras();
        int weaponId = extras.getInt("weaponId");
        Log.d("dev", "Weapon Id: " + weaponId);

        WeaponModel weaponModel = getDb().getWeaponById(weaponId);
        if(weaponModel == null){
            this.finish();
            return;
        }

        this.setTitle(String.format("R%s - %s (%s)", weaponModel.getRarity(), weaponModel.getName(), getDb().getWeaponTypeShortNameById(weaponModel.getWeaponTypeId())));

        populateWeaponDetailsView(weaponModel);
        setVisibility();
    }

    private void setVisibility() {
        toggleVisibility(R.id.descriptionRow, false);
        toggleVisibility(R.id.upgradeRow, false);
    }

    private void populateWeaponDetailsView(WeaponModel weaponModel) {
        setTextToId(weaponModel.getRawDamage(), R.id.rawDamage);

        ElementModel element = getDb().getElementById(weaponModel.getElementId());
        setImageToId(element == null ? 0 : element.getIcon(), R.id.elementIcon);
        int elementDamage = weaponModel.getElementDamage();
        setTextToId(elementDamage == 0 ? "-" : String.valueOf(elementDamage), R.id.elementDamage);

        setTextToId(weaponModel.getAffinity(), R.id.affinity);
        setTextToId(weaponModel.getDefense(), R.id.def);

        setTextToId(weaponModel.getSlotCount(), R.id.slots);

        setImageToId(weaponModel.getSharpnessImage(), R.id.sharpnessIcon);

        setTextToId(Formatter.getPriceFormat(weaponModel.getBuyingPrice()), R.id.buyPrice);
        setTextToId(Formatter.getPriceFormat(weaponModel.getSellingPrice()), R.id.sellPrice);

        setTextToId(weaponModel.getDescription(), R.id.description);

        fillUpgradeFromDetails(weaponModel.getId());
        fillUpgradeToDetails(weaponModel.getId());
    }

    private void fillUpgradeFromDetails(int weaponId) {
        WeaponUpgradeModel weaponUpgradeFromModel = WeaponUpgradeList.getWeaponUpgradeFromById(weaponId);
        ArrayList<WeaponUpgradeModel> modelList = new ArrayList<WeaponUpgradeModel>(1);
        modelList.add(weaponUpgradeFromModel);
        fillUpgradeDetails(R.id.upgradeFromDetails, modelList);
    }

    private void fillUpgradeToDetails(int weaponId){
        ArrayList<WeaponUpgradeModel> modelList = WeaponUpgradeList.getWeaponUpgradeToById(weaponId);
        fillUpgradeDetails(R.id.upgradeToDetails, modelList);
    }

    private void fillUpgradeDetails(int viewId, ArrayList<WeaponUpgradeModel> modelList){
        if(modelList.size() == 0){
            modelList.add(null);
        }
        ListView upgradeDetails = (ListView)findViewById(viewId);
        WeaponUpgradeAdapter adapter = new WeaponUpgradeAdapter(WeaponDetailsActivity.this, modelList);
        upgradeDetails.setAdapter(adapter);
    }

    private void setImageToId(int icon, int id) {
        ImageView field = (ImageView)findViewById(id);
        field.setImageResource(icon);
    }

    private void setTextToId(String text, int id) {
        TextView field = (TextView)findViewById(id);
        field.setText(text);
    }

    private void setTextToId(int text, int id) {
        setTextToId(String.valueOf(text), id);
    }

    private void toggleVisibility(int id, boolean setVisible){
        toggleVisibility(findViewById(id), setVisible);
    }

    private void toggleVisibility(View row, boolean setVisible){
        row.setVisibility(setVisible ? View.VISIBLE : View.GONE);
    }

    private void toggleVisibility(int id){
        View row = findViewById(id);
        toggleVisibility(row, !row.isShown());
    }

    public void toggleDescriptionVisibility(View view) {
        toggleVisibility(R.id.descriptionRow);
    }

    public void toggleUpgradeVisibility(View view) {
        toggleVisibility(R.id.upgradeRow);
    }

}
